<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Worker; 

class WGrouprank extends Base
{
    public function W_grouprank()
    {
global $_G, $_P;
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('管理后台地址不正确');
}
$system=$_G['system'];
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
if(empty($theme['id'])){
	return_msg('管理后台地址不正确');
}
$myworker = Worker::getThemeWorkerByOpenidNew($_G['openid'],$themeid);
if(!($system['ruanjian']!=3 || $config['showshop']==0 || ($system['ruanjian']==3 && $config['showshop']==$myworker['company']))){
	$myworker = "";
}
if (empty($myworker)) {
	return_msg('您不是员工,无权访问');
}
$msg = Worker::gethoutaistats();
if (!empty($msg)) {
	return_msg($msg);
}

if ($myworker['level'] == '1') {
	$company = empty($_P['company'])?$myworker['company']:$_P['company'];
	$shop=db_find("SELECT * FROM ".tablename(t_shangjia)." WHERE id= '{$company}'");
	$shopname=$shop['name'];
} else {
	$company = $myworker['company'];
}
if ($config['isrank']==0 && $myworker['level'] == '3'){
	return_msg('无权访问');
}
if ($myworker['level'] == '3') {
	$team=db_find("SELECT * FROM ".tablename(t_team)." WHERE id= '{$myworker['team']}'");
	$teamname=$team['name'];
}

$op = $_P['op'];
$optype = $_P['type'];
if ($op!='report') {
	$do = 'w_grouprank';
	$paixu = $_P['paixu'];
	if ($paixu == 'xfs') {
		$paixutxt = "order by newfans desc,record desc";
	} elseif ($paixu == 'zf') {
		$paixutxt = "order by forward desc,record desc";
	} elseif ($paixu == 'll') {
		$paixutxt = "order by scansum desc,record desc";
	} elseif ($paixu == 'fee') {
		$paixutxt = "order by fee desc,record desc";
	} elseif ($paixu == 'xx') {
		$paixutxt = "order by recordxx desc,record desc";
	} else {
		$paixutxt = "order by record desc,recordxx desc";
	}
	if ($config['showalldata']==0) {
		$noalldata = " and w.themeid =" .$themeid;
	}
	$notworker = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
		WHERE status > 0  and status!=3 and workerid = '' and themeid = '{$themeid}' " );
	if ($op != 'list') {
		$paixuteam = $paixutxt .",t.id";
	}else{
		$paixuteam = "order by t.id";
	}
	// $team = db_select("SELECT t.name , w.team, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
	// 	left join " . tablename ( t_team ) . " t on w.team = t.id
	// 	left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
	// 	WHERE 1=1 " . $noalldata . " and w.company = " . $company ." group by w.team " . $paixuteam  );

	// $sql1 = "SELECT t.name ,w.team, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
	// 	left join " . tablename ( t_team ) . " t on w.team = t.id
	// 	left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
	// 	WHERE 1=1 " . $noalldata . " and w.company = " . $company ." and r.riqi='";
	// $sql2 = "' group by w.team " . $paixutxt .",t.id";

	// $teamtoday = db_select($sql1.date("Y-m-d").$sql2);
	// $sql = $sql1.date("Y-m-d",strtotime("-1 day")).$sql2;
	// $teamyesterday = db_select($sql1.date("Y-m-d",strtotime("-1 day")).$sql2);
	// $teambeforeyesterday = db_select($sql1.date("Y-m-d",strtotime("-2 day")).$sql2);

	$sql1 = "SELECT t.name , w.team, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
		left join " . tablename ( t_team ) . " t on w.team = t.id
		left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
		WHERE 1=1 " . $noalldata . " and w.mobile<>'' and w.company = " . $company ;
	$sql2 = " group by w.team " . $paixutxt .",t.id";

	$team = db_select($sql1.$sql2);
	$teamtoday = db_select($sql1 . " and r.riqi='".date("Y-m-d")."' ". $sql2);
	$teamyesterday = db_select($sql1 . " and r.riqi='".date("Y-m-d",strtotime("-1 day"))."' ". $sql2);
	$teambeforeyesterday = db_select($sql1 . " and r.riqi='".date("Y-m-d",strtotime("-2 day"))."' ". $sql2);

	foreach ($team as $index => $item) {
		$list = db_select("SELECT w.username as name,w.uid,w.team, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
			left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
			WHERE 1=1 " . $noalldata . " and w.mobile<>'' and w.company = " . $company ." and w.team = " . $item['team'] ." group by w.uid " . $paixutxt .",w.uid" );
		$team[$index]['list']=$list;
	}

	$sql1 = "SELECT w.username as name ,w.uid,w.team, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
				left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
				WHERE w.mobile<>'' and w.company = " . $company ." " . $noalldata . " and w.team = " ;
	$sql2 = "' group by w.uid " . $paixutxt .",w.uid";

	foreach ($teamtoday as $index => $item) {
		$list = db_select($sql1. $item['team'] ." and r.riqi='".date("Y-m-d").$sql2);
		$teamtoday[$index]['list']=$list;
	}
	foreach ($teamyesterday as $index => $item) {
		$list = db_select($sql1. $item['team'] ." and r.riqi='".date("Y-m-d",strtotime("-1 day")).$sql2);
		$teamyesterday[$index]['list']=$list;
	}
	foreach ($teambeforeyesterday as $index => $item) {
		$list = db_select($sql1. $item['team'] ." and r.riqi='".date("Y-m-d",strtotime("-2 day")).$sql2);
		$teambeforeyesterday[$index]['list']=$list;
	}
}
if ($op=='list') {
	if ($optype == 'all') {
		$ranktips ='总排行';
	}elseif ($optype == 'today') {
		$ranktips ='今日排行';
		$team = $teamtoday;
	}elseif ($optype == 'yesterday') {
		$ranktips ='昨日排行';
		$team = $teamyesterday;
	}elseif ($optype == 'beforeyesterday') {
		$ranktips ='前日排行';
		$team = $teambeforeyesterday;
	}
}elseif ($op=='report' && $myworker['level'] != '3') {
	if ($myworker['level'] == '1') {
		$sql = 'SELECT w.*,t.name as teamname,s.name as companyname FROM ' . tablename(t_worker) . " as w 
				left join " . tablename ( t_team ) . " as t on w.team = t.id
				left join " . tablename ( t_shangjia ) . " as s on w.company = s.id
				WHERE w.mobile<>'' and w.themeid = :themeid
				ORDER BY company,team,uid  ";
		$wlist = db_select($sql, array(":themeid"=>$themeid));	
	}else{
		$sql = 'SELECT w.*,t.name as teamname,s.name as companyname FROM ' . tablename(t_worker) . " as w 
				left join " . tablename ( t_team ) . " as t on w.team = t.id
				left join " . tablename ( t_shangjia ) . " as s on w.company = s.id
				WHERE w.mobile<>'' and w.themeid = :themeid AND w.company = :company
				ORDER BY company,team,uid  ";
		$wlist = db_select($sql, array(":themeid"=>$themeid,":company"=>$company));	
	}
	$uid = $_P['uid'];
	$info = Worker::getThemeWorkerByUid($_P['uid'],$themeid);
	if ($config['showalldata']==0) {
		$report = db_select("SELECT * FROM " . tablename ( t_workerreport ) . " WHERE themeid ='{$themeid}' and workerid = '{$uid}' order by riqi desc" );
	}else{
		$report = db_select("SELECT riqi,sum(newfans) as newfans,sum(forward) as forward,sum(scansum) as scansum,sum(recordzx) as recordzx,sum(record) as record,sum(recordxx) as recordxx,sum(fee) as fee,sum(comm) as comm FROM " . tablename ( t_workerreport ) . " WHERE workerid = '{$uid}' group by riqi order by riqi desc" );
	}
}

if($_G['companyname'] =='新飞驰软件'){
	$return_clicent = get_defined_vars();
return view('/fcworker/fc_grouprank' , $return_clicent);
}else{
	$return_clicent = get_defined_vars();
return view('/yxworker/w_grouprank' , $return_clicent);
}
}
}